(function (G) {
    G.vue({
        data: {
          imgList:[],
          list:[],
          all:0,
          num:0,
          top:wx.getSystemInfoSync().statusBarHeight + wx.getMenuButtonBoundingClientRect().top,
          ydCode:0,
          currentTime:'',
          FourList:[]
        },
        methods: {
            // 计算两点距离
            demo(lat1, lng1, lat2, lng2){
                lat1 = lat1 || 0;
                lng1 = lng1 || 0;
                lat2 = lat2 || 0;
                lng2 = lng2 || 0;
                var rad1 = lat1 * Math.PI / 180.0;
                var rad2 = lat2 * Math.PI / 180.0;
                var a = rad1 - rad2;
                var b = lng1 * Math.PI / 180.0 - lng2 * Math.PI / 180.0;
                var r = 6378137;
                var distance = r * 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) + Math.cos(rad1) * Math.cos(rad2) * Math.pow(Math.sin(b / 2), 2)));
                return distance;
            },
            // 将数据按100米分类
            handleimgUrls(){
                let dataArr = []
                let list = this.data.imgList
                list.map((mapItem) => {
                    if (dataArr.length == 0) {
                    dataArr.push({ 
                        latitude: mapItem.latitude,
                        longitude: mapItem.longitude,
                        address:mapItem.address, 
                        createTimeString:mapItem.createTimeString,
                        List: [mapItem] })
                    } else {
                    let res = dataArr.some((item) => {
                        let num1 = Number(item.latitude);
                        let a1 = num1.toFixed(15);
                        let num2 = Number(item.longitude);
                        let a2 = num2.toFixed(15);
                        let num3 = Number(mapItem.latitude);
                        let a3 = num3.toFixed(15);
                        let num4 = Number(mapItem.longitude);
                        let a4 = num4.toFixed(15);
                        let num = this.demo(a1, a2, a3, a4)
                        if (num < 100) {
                            item.List.push(mapItem)
                            return true
                        }
                    })
                    if (!res) {
                        dataArr.push({ 
                            latitude: mapItem.latitude,
                            longitude: mapItem.longitude, 
                            address:mapItem.address,
                            createTimeString:mapItem.createTimeString,
                            List: [mapItem]})
                    }
                    }
                })
                this.setData({
                    list:dataArr
                })
            },
            //预览图片，放大预览
            preview(event) {
                let url = event.currentTarget.dataset.url
                wx.previewImage({
                    current: url,
                    urls:[url]
                })
            },
            // 获取缓存的imgList
            getimgList(){
              let array = wx.getStorageSync('imgList')
              let num = 0
              let all = 0
              let currentTime = this.data.currentTime
              let diffTime = ''
              for (let index = 0; index < array.length; index++) {
                if (array[index].code == 1)num++
              }
              for (let i = 0; i < array.length; i++) {
                let old = new Date(array[i].time)
                let now = new Date(currentTime)
                diffTime = now.getTime() - old.getTime()
                const minute = Math.floor(diffTime / (1000 * 60))
                array[i].diffTime = minute
              }
              if (num == array.length) {
                all = 1
              }
              this.setData({
                imgList: array || [],
                num,
                all
              })
              this.handleimgUrls()
            },
            // 更改code值  code=0表示该图片未选中，code=1表示该图片被选中
            updataCode(e){
              let that = this
              let index1 = e.currentTarget.dataset
              let imgindex = Number(index1.imgindex)
              let index = Number(index1.index)
              let i = that.data.list
              let j = that.data.imgList
              let num = that.data.num
              let all
              i[imgindex].List[index].code == 0 ? (i[imgindex].List[index].code = 1)+(num++) :(i[imgindex].List[index].code = 0 )+(num--)
              for (let e = 0; e < j.length; e++) {
                if (j[e].url == i[imgindex].List[index].url) {
                  j[e].code = i[imgindex].List[index].code
                }
              } 
              if (num == j.length) {
                all = 1
              }else{
                all = 0
              }
              that.setData({
                list:i,
                imgindex:j,
                num,
                all
              })
              wx.setStorage({
                key: 'imgList',
                data: j
              })
              
            },
            // 全选
            allCode1(){
                let that = this
                let j = that.data.imgList
                for (let e = 0; e < j.length; e++) {
                    j[e].code = 1
                } 
                wx.setStorage({
                  key: 'imgList',
                  data: j
                })
                // 加延时器是为了解决手机缓存更新慢的问题
                setTimeout(() => {
                    this.onShow()
                }, 100);
            },
            // 取消全选
            allCode0(){
                let that = this
                let j = that.data.imgList
                for (let e = 0; e < j.length; e++) {
                    j[e].code = 0
                } 
                wx.setStorage({
                  key: 'imgList',
                  data: j
                })
                // 加延时器是为了解决手机缓存更新慢的问题
                setTimeout(() => {
                    this.onShow()
                }, 100);
            },
            // 提示必须选择一张照片
            mustSelect(){
                wx.showToast({
                    title: '请至少选择一张照片',
                    icon:'none'
                })
            },
            // 删除code为1的数据
            delPic(){
                let that = this
                wx.showModal({
                    title: '请确认是否删除',
                    success (res) {
                        if (res.confirm) {
                            // console.log('用户点击确定')
                            let arr = []
                            let j = that.data.imgList
                            for (let i = 0; i < j.length; i++) {
                                if (j[i].code == 1) {
                                arr.push(j[i])
                                j.splice(i--, 1);
                                }
                            }
                            wx.setStorage({
                                key: 'imgList',
                                data: j
                            })
                            // 加延时器是为了解决手机缓存更新慢的问题
                            setTimeout(() => {
                                // arr是删除的数据，i是删除之前的数据，j是删除之后的数据
                                wx.showToast({
                                    title: '删除成功',
                                })
                                that.setData({
                                    ydCode:false,
                                })
                                that.onShow()
                            }, 100);
                        } else if (res.cancel) {
                            // console.log('用户点击取消')
                            that.allCode0()
                        }
                    }
                })
            },
            goto_one(){
                let that = this
                let j = that.data.imgList
                let newClickedList = [],newUnClickedList = [],time = new Date().getTime()
                for (let i = 0; i < j.length; i++) {
                    if (j[i].code == 1) {
                        if(j[i].time && j[i].time < time) time = j[i].time
                        newClickedList.push(j[i]);
                    }else{
                        newUnClickedList.push(j[i])
                    }
                }
                newClickedList.time = time
                G.Storage.set('newClickedList',newClickedList)
                G.Storage.set('newUnClickedList',newUnClickedList)
                // wx.removeStorageSync('imgList')
                let type = that.data.FourList[0].type
                if(type == 1){
                    wx.navigateTo({
                        url: '/pages/check/write/index?type=' + type
                    })
                }else{
                    wx.navigateTo({
                        url: '/pages/gycase/addAndKoufen/index?type=' + type
                    })
                }
                that.allCode0()
            },
            // 携带code为1的数据跳转页面
            goto(){
              let that = this
              let list = that.data.FourList
              let itemList = []
              for (let i = 0; i < list.length; i++) {
                itemList[i] = list[i].name
              }
              wx.showActionSheet({
                itemList: itemList,
                success (res) {
                    let tapIndex = res.tapIndex
                    let j = that.data.imgList
                    let newClickedList = [],newUnClickedList = [],time = new Date().getTime()
                    for (let i = 0; i < j.length; i++) {
                        if (j[i].code == 1) {
                            if(j[i].time && j[i].time < time) time = j[i].time
                            newClickedList.push(j[i]);
                        }else{
                            newUnClickedList.push(j[i])
                        }
                    }
                    newClickedList.time = time
                    G.Storage.set('newClickedList',newClickedList)
                    G.Storage.set('newUnClickedList',newUnClickedList)
                    let type = that.data.FourList[tapIndex].type
                    if(type == 1){
                        wx.navigateTo({
                            url: '/pages/check/write/index?type=' + type
                        })
                    }else{
                        wx.navigateTo({
                            url: '/pages/gycase/addAndKoufen/index?type=' + type
                        })
                    }
                    // wx.removeStorageSync('imgList')
                    that.allCode0()
                },
                fail (res) {
                  console.log(res.errMsg)
                }
              })
              // console.log(newList);
             
            },
            // 选择的状态值，为0不出现圆点，为1出现圆点
            updataYdcode(){
                let data = this.data.ydCode
                data == 0 ? data = 1 : data = 0
                this.setData({
                    ydCode:data
                })
            },
             // 格式化时间
            formatDate(times) {
                var date = new Date(times);
                var year = date.getFullYear().toString(); //年份
                var month = (date.getMonth() + 1).toString(); //月份
                var day1 = date.getDate(); //日
                if (day1.toString().length == 1) {
                    var day = '0' + day1.toString()
                } else {
                    var day = day1.toString()
                }
                var hour = date.getHours() < 10 ? '0' + date.getHours() : date.getHours()
                var minute = date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes();
                var second = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds();
                return month + '/' + day +  '/' + year + ' ' + hour + ':' + minute + ':' + second
                //   year + '-' + month + '-' + day + ' ' + hour + ':' + minute + ':' + second
            },
            // 获取实时时间
            getCurrentTime() {
                let that = this
                let time1 = new Date();
                // console.log('----time1', time1);
                let time2 = that.formatDate(time1)
                // console.log('----time2', time2);
                that.setData({
                    currentTime: time2
                })
                // console.log('---currentTime', that.data.currentTime);
                if (that.data.timeController) {
                    setTimeout(() => {
                        that.getCurrentTime()
                    }, 1000)
                }
            },
            // 用户有那些type（1,2,3,4）
            fourList(){
                let list = wx.getStorageSync('FourList')
                let arr = [
                    {id:1599287462763565056,name:'日常巡查',type:1},
                    {id:1599292417239027712,name:'月度考核',type:2},
                    {id:1597262949767057408,name:'公众监督',type:3},
                    {id:1597263034395529216,name:'重大事件',type:4},
                ]
                let FourList = []
                for (let i = 0; i < list.length; i++) {
                    for (let j = 0; j < arr.length; j++) {
                        if (arr[j].id == list[i]) {
                            FourList.push(arr[j])
                        }
                    }
                }
                this.setData({FourList})
            },
            back(){
                wx.navigateBack({
                  delta: 1,
                })
            },
            onShow(){
                this.setData({
                    timeController: true,
                })
                this.getimgList()
                this.getCurrentTime()
                this.fourList()
            },
            onHide() {
                this.setData({
                    timeController: false
                })
            },
        },
        mounted(query) {
            this.onShow()
            this.setData({
                ydCode:false
            })
        }
    })
})(wx.$)